// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp');  // yarn init --yes     yarn add gulp --dev
const del = require('del')                // yarn add del --dev
const browserSync = require('browser-sync');      // 热更新

const loadplugins = require('gulp-load-plugins');
const plugins = loadplugins();

// const plugins.sass = require('gulp-sass');      // yarn add gulp-sass --dev     
// const plugins.babel = require('gulp-babel');    // yarn add gulp-babel --dev
// const plugins.swig = require('gulp-swig');    // yarn add gulp-swig --dev     
// const plugins.imagemin = require('gulp-imagemin');    // yarn add gulp-swig --dev   



const bs = browserSync.create();

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const clean = () => {
  return del(['dist'])
} 

const style = ()=>{
  return src('src/assets/styles/*.scss', { base: 'src'})
        .pipe( plugins.sass({ outputStyle: 'expanded' }) )
        .pipe( dest('dist') )
}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe( plugins.babel( { presets: ['@babel/preset-env'] } ) )
        .pipe( dest('dist') )
}

const page = () => {
  return src('src/**/*.html', { base: 'src'})
        .pipe( plugins.swig({ data }) )
        .pipe( dest('dist') )
}

const image = () => {
  return src('src/assets/images/**', { base: 'src'})
        .pipe( plugins.imagemin() )
        .pipe( dest('dist') )
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src'})
        .pipe( plugins.imagemin() )
        .pipe( dest('dist') )
}

const extra = () => {
  return src('public/**', {base: 'public'})
        .pipe(dest('dist'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style )
  watch('src/assets/styles/*.scss', script )
  watch('src/**/*.html', page )
  // watch('src/assets/images/**', image )
  // watch('src/assets/fonts/**', font )
  // watch('public/**', extra )
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'src/**/*.html'
  ], bs.reload)
  
  bs.init({
    notify:false,
    port: 3000,
    files: 'dist/**',
    server: {
      baseDir: 'dist',
      toutes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const useref = () =>　{
  return src('dist/*.html', {base: 'dist'})
        .pipe(plugins.useref({ searchPath: ['dist', '.']}))
        .pipe(dest('dist'))
}

const compile = parallel(style, script, page, image, font);
const build = series(clean, parallel(compile, extra));
const develop = series(compile,  serve)

module.exports = {
  compile,
  build,
  develop,
  useref
}